using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Mapping.Attributes;
using Lib;

namespace Persistance.Model
{

    /**
     * Problem Causes Specification:
     * 
     * ProblemCausesID (int) --> The unique identifier for each Problem Cause
     * ProblemID (int) --> The unique idenfier that relates back to a specific Problem. Cannot be null
     * Cause (text) --> The description of the cause
     * 
     * WhatExplain (text) --> How the cause explains the What
     * WhereExplain (text) --> How the cause explains the Where
     * WhenExplain (text) --> How the cause explains the When
     * ExtentExplain (text) --> How the cause explains the Extent
     * 
     * WhatExplainsOnlyIf (text) --> When does the cause explain the What?
     * WhereExplainsOnlyIf (text) --> When does the cause explain the Where?
     * WhenExplainsOnlyIf (text) --> When does the cause explain the When?
     * ExtentExplainsOnlyIf (text) --> When does the cause explain the Extent?
     * 
     * IsProbableCause (bit) --> Binary value denoting if the Cause is the Probable Cause
     * AuthorID (string) --> Identifies the original Author of the Possible Cause
     * LastEditedBy (string) --> Identifies the Author that last made changes to the Problem Cause
     * CreatedOn (datetime) --> The Date the Problem Cause was created
     * EdittedOn (datetime) --> The Date of the last edit to the Problem Cause
     * 
     * */

    [Serializable]
    [Class]
    public class ProblemCauses
    {

        private int _problemCauseID;
        //NHIBER: Setts the primary key to ID
        [Id(Name = "ProblemCauseID")]
        [Generator(1, Class = "native")]
        public virtual int ProblemCauseID
        {
            get { return _problemCauseID; }
            set { _problemCauseID = value; }
        }

        /// <summary>
        /// Gets or Sets the Description of the Problem Cause
        /// </summary>
        private string _cause;
        [Property(Type = "StringClob", Length = 5000)]
        public virtual string Cause
        {
            get { return _cause; }
            set { _cause = value; }
        }

        /// <summary>
        /// Gets or Sets the Problem that this Problem Cause associated with
        /// </summary>
        private Problem _problem;
        public virtual Problem Problem
        {
            get { return _problem; }
            set { _problem = value; }
        }


        #region " Explains Fields "

        /// <summary>
        /// Gets or Sets the What Explanation Field of the Problem Cause
        /// </summary>
        private string _whatExplain;
        [Property(Type = "StringClob", Length = 5000)]
        public virtual string WhatExplain
        {
            get { return _whatExplain; }
            set { _whatExplain = value; }
        }

        /// <summary>
        /// Gets or Sets the Where Explanation Field of the Problem Cause
        /// </summary>
        private string _whereExplain;
        [Property(Type = "StringClob", Length = 5000)]
        public virtual string WhereExplain
        {
            get { return _whereExplain; }
            set { _whereExplain = value; }
        }

        /// <summary>
        /// Gets or Sets the When Explanation Field of the Problem Cause
        /// </summary>
        private string _whenExplain;
        [Property(Type = "StringClob", Length = 5000)]
        public virtual string WhenExplain
        {
            get { return _whenExplain; }
            set { _whenExplain = value; }
        }


        /// <summary>
        /// Gets or Sets the Extent Explanation Field of the Problem Cause
        /// </summary>
        private string _extentExplain;
        [Property(Type = "StringClob", Length = 5000)]
        public virtual string ExtentExplain
        {
            get { return _extentExplain; }
            set { _extentExplain = value; }
        }
        #endregion

        #region " Explains Only If Fields "

        /// <summary>
        /// Gets or Sets the What Only If Explanation Field of the Problem Cause
        /// </summary>
        private string _whatExplainOnlyIf;
        [Property(Type = "StringClob", Length = 5000)]
        public virtual string WhatExplainOnlyIf
        {
            get { return _whatExplainOnlyIf; }
            set { _whatExplainOnlyIf = value; }
        }

        /// <summary>
        /// Gets or Sets the Where Only If Explanation Field of the Problem Cause
        /// </summary>
        private string _whereExplainOnlyIf;
        [Property(Type = "StringClob", Length = 5000)]
        public virtual string WhereExplainOnlyIf
        {
            get { return _whereExplainOnlyIf; }
            set { _whereExplainOnlyIf = value; }
        }

        /// <summary>
        /// Gets or Sets the When Only If Explanation Field of the Problem Cause
        /// </summary>
        private string _whenExplainOnlyIf;
        [Property(Type = "StringClob", Length = 5000)]
        public virtual string WhenExplainOnlyIf
        {
            get { return _whenExplainOnlyIf; }
            set { _whenExplainOnlyIf = value; }
        }


        /// <summary>
        /// Gets or Sets the Extent Only If Explanation Field of the Problem Cause
        /// </summary>
        private string _extentExplainOnlyIf;
        [Property(Type = "StringClob", Length = 5000)]
        public virtual string ExtentExplainOnlyIf
        {
            get { return _extentExplainOnlyIf; }
            set { _extentExplainOnlyIf = value; }
        }

        #endregion


        private Boolean _isProbableCause;
        [Property(Type = "Boolean")]
        public virtual Boolean IsProbableCause
        {
            get { return _isProbableCause; }
            set { _isProbableCause = value; }
        }

        private Users _author = null;
        [ManyToOne(Column = "AuthorID", ForeignKey = "FK_ProblemCausesAuthor")]
        public virtual Users Author
        {
            get { return _author; }
            set { _author = value; }
        }

        private Users _lastUpdatedBy = null; 
        [ManyToOne(Column = "LastUpdatedBy", ForeignKey = "FK_LastUpdatedByAuthor")]
        public virtual Users LastUpdatedBy
        {
            get { return _lastUpdatedBy; }
            set { _lastUpdatedBy = value; }
        }

        private DateTime _createdOn = new DateTime(2008, 1, 1);
        [Property(Type = ("DateTime"))]
        public virtual DateTime CreatedOn
        {
            get { return _createdOn; }
            set { _createdOn = value; }
        }

        private DateTime _edittedOn = new DateTime(2008, 1, 1);
        [Property(Type = ("DateTime"))]
        public virtual DateTime EdittedOn
        {
            get { return _edittedOn; }
            set { _edittedOn = value; }
        }


        /// <summary>
        /// Gets the all information on the current Problem Cause
        /// </summary>
        /// <example>
        /// <code>
        /// ProblemCause pc = new ProblemCause();
        /// pc.ProblemCauseID = id;
        /// pc.Get();
        /// </code>
        /// </example>
        /// <returns> A list of Problem Causes. The List contains one member but is used for data access purposes. Makes it easier :-)</returns>
        public virtual List<ProblemCauses> Get()
        {
            ISession sess = Persistor.CurrentSession;
            try
            {
                IQuery q = sess.CreateQuery("from ProblemCauses pc where pc.ProblemCauseID = :ID");
                q.SetInt32("ID", _problemCauseID);
                List<ProblemCauses> results = (List<ProblemCauses>)q.List<ProblemCauses>();

                return results;
            }
            catch (Exception e)
            {
                throw new Exception("akljsghp", e);
            }
        }

        /// <summary>
        /// Persists the current Problem Cause to the db
        /// </summary>
        public virtual void Create()
        {
            ISession sess = Persistor.CurrentSession;
            try
            {
                sess.Transaction.Begin();
                sess.Save(this);
                sess.Transaction.Commit();
                sess.Flush();
            }
            catch (Exception e)
            {
                sess.Transaction.Rollback();
                throw new Exception("akljsghp", e);
            }
        }
    }
}
